/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.world;

import java.io.IOException;

import org.newdawn.slick.SlickException;

import ca.usherbrooke.behaviours.world.BasicWorldResponder;
import ca.usherbrooke.behaviours.world.SubscriptionEntityModel;
import ca.usherbrooke.behaviours.world.SubscriptionWorldModel;
import ca.usherbrooke.behaviours.world.WorldConfigurationResponderBehaviour;
import ca.usherbrooke.behaviours.world.WorldUpdaterBehaviour;
import ca.usherbrooke.builder.SimulationWrapper;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public class WorldAgentStandard extends BasicWorldAgent {
	private static final long serialVersionUID = 1L;

	private static int STANDARD_UPDATE_TIME = 50;
	public static final String DESCRIPTION_TYPE_WORLD_MODEL = "world_model";

	private SimulationWrapper simulation;
	private WorldUpdaterBehaviour worldUpdater;

	@Override
	protected void setup() {
		super.setup();
		// we setup the world
		final String filename = (String) this.getArguments()[argumentsIdx + 0];
		argumentsIdx += 1;

		try {
			simulation = new SimulationWrapper(filename);
			worldUpdater = new WorldUpdaterBehaviour(this, STANDARD_UPDATE_TIME);

			// we add our behaviour
			this.addBehaviour(new SubscriptionEntityModel(this));
			this.addBehaviour(new BasicWorldResponder(this));
			this.addBehaviour(new SubscriptionWorldModel(this));

			this.addBehaviour(new WorldConfigurationResponderBehaviour(this));
			this.addBehaviour(worldUpdater);
		} catch (SlickException ex1) {
			ExceptionTool.showError(
					"The agent that update the world will deleted", ex1);
			this.doDelete();
		} catch (IOException ex1) {
			ExceptionTool.showError(
					"The agent that update the world will deleted", ex1);
			this.doDelete();
		}
	}

	@Override
	public void startUpdate() {
		worldUpdater.setDelta(STANDARD_UPDATE_TIME);
	}

	@Override
	public void setDelta(Integer newDelta) {
		worldUpdater.setDelta(newDelta);
	}

	@Override
	public Integer getDelta() {
		return worldUpdater.getDelta();
	}

	@Override
	public IWorldModel getWorld() {
		return simulation.getWorld();
	}

	@Override
	public SimulationWrapper getSimulation() {
		return this.simulation;
	}
}
